package flc.ast.adapter;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.text.SimpleDateFormat;

import flc.ast.R;
import flc.ast.bean.SelectFileBean;
import flc.ast.bean.TransferRecordBean;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.TimeUtil;

public class RecordKindAdapter extends StkProviderMultiAdapter<SelectFileBean> {

    public RecordKindAdapter() {
        addItemProvider(new StkSingleSpanProvider<>(40));
        addItemProvider(new RecordKindProvider());
    }

    private boolean isEdit;

    public void setEdit(boolean edit) {
        isEdit = edit;
    }

    private class RecordKindProvider extends BaseItemProvider<SelectFileBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_record_kind;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, SelectFileBean bean) {
            switch (bean.getType()) {
                case 1:
                    baseViewHolder.getView(R.id.rlImgItem).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.rlVideoItem).setVisibility(View.GONE);
                    baseViewHolder.getView(R.id.llAudioItem).setVisibility(View.GONE);
                    baseViewHolder.getView(R.id.llAppItem).setVisibility(View.GONE);
                    baseViewHolder.getView(R.id.llContactItem).setVisibility(View.GONE);
                    Glide.with(getContext()).load(bean.getPath())
                            .into((ImageView) baseViewHolder.getView(R.id.ivImgItemImg));
                    if (bean.isSelected()) {
                        baseViewHolder.setImageResource(R.id.ivImgItemSel, R.drawable.axuanz);
                    } else {
                        baseViewHolder.setImageResource(R.id.ivImgItemSel, R.drawable.aweixuanzhong);
                    }
                    if (isEdit) {
                        baseViewHolder.getView(R.id.ivImgItemSel).setVisibility(View.VISIBLE);
                    } else {
                        baseViewHolder.getView(R.id.ivImgItemSel).setVisibility(View.GONE);
                    }
                    break;
                case 2:
                    baseViewHolder.getView(R.id.rlImgItem).setVisibility(View.GONE);
                    baseViewHolder.getView(R.id.rlVideoItem).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.llAudioItem).setVisibility(View.GONE);
                    baseViewHolder.getView(R.id.llAppItem).setVisibility(View.GONE);
                    baseViewHolder.getView(R.id.llContactItem).setVisibility(View.GONE);
                    Glide.with(getContext()).load(bean.getPath())
                            .into((ImageView) baseViewHolder.getView(R.id.ivVideoItemImg));
                    baseViewHolder.setText(R.id.tvVideoItemDuration, TimeUtil.getMmss(MediaUtil.getDuration(bean.getPath())));
                    if (bean.isSelected()) {
                        baseViewHolder.setImageResource(R.id.ivVideoItemSel, R.drawable.axuanz);
                    } else {
                        baseViewHolder.setImageResource(R.id.ivVideoItemSel, R.drawable.aweixuanzhong);
                    }
                    if (isEdit) {
                        baseViewHolder.getView(R.id.ivVideoItemSel).setVisibility(View.VISIBLE);
                    } else {
                        baseViewHolder.getView(R.id.ivVideoItemSel).setVisibility(View.GONE);
                    }
                    break;
                case 3:
                    baseViewHolder.getView(R.id.rlImgItem).setVisibility(View.GONE);
                    baseViewHolder.getView(R.id.rlVideoItem).setVisibility(View.GONE);
                    baseViewHolder.getView(R.id.llAudioItem).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.llAppItem).setVisibility(View.GONE);
                    baseViewHolder.getView(R.id.llContactItem).setVisibility(View.GONE);
                    baseViewHolder.setText(R.id.tvSelAudioItemDate, TimeUtils.millis2String(FileUtils.getFileLastModified(bean.getPath()), new SimpleDateFormat("yyyy年MM月dd日")));
                    if (bean.isSelected()) {
                        baseViewHolder.setImageResource(R.id.ivSelAudioItemSel, R.drawable.axuanz);
                    } else {
                        baseViewHolder.setImageResource(R.id.ivSelAudioItemSel, R.drawable.aweixuanzhong);
                    }
                    if (isEdit) {
                        baseViewHolder.getView(R.id.ivSelAudioItemSel).setVisibility(View.VISIBLE);
                    } else {
                        baseViewHolder.getView(R.id.ivSelAudioItemSel).setVisibility(View.GONE);
                    }
                    break;
                case 4:
                    baseViewHolder.getView(R.id.rlImgItem).setVisibility(View.GONE);
                    baseViewHolder.getView(R.id.rlVideoItem).setVisibility(View.GONE);
                    baseViewHolder.getView(R.id.llAudioItem).setVisibility(View.GONE);
                    baseViewHolder.getView(R.id.llAppItem).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.llContactItem).setVisibility(View.GONE);
                    baseViewHolder.setText(R.id.tvAppItemName, FileUtils.getFileName(bean.getPath()));
                    baseViewHolder.setText(R.id.tvAppItemCreate, FileUtils.getSize(AppUtils.getAppPath(bean.getPath())));
                    baseViewHolder.setImageDrawable(R.id.ivAppItemImg, getApkIcon(getContext(), bean.getPath()));
                    if (bean.isSelected()) {
                        baseViewHolder.setImageResource(R.id.ivAppItemSel, R.drawable.axuanz);
                    } else {
                        baseViewHolder.setImageResource(R.id.ivAppItemSel, R.drawable.aweixuanzhong);
                    }
                    if (isEdit) {
                        baseViewHolder.getView(R.id.ivAppItemSel).setVisibility(View.VISIBLE);
                    } else {
                        baseViewHolder.getView(R.id.ivAppItemSel).setVisibility(View.GONE);
                    }
                    break;
                case 5:
                    baseViewHolder.getView(R.id.rlImgItem).setVisibility(View.GONE);
                    baseViewHolder.getView(R.id.rlVideoItem).setVisibility(View.GONE);
                    baseViewHolder.getView(R.id.llAudioItem).setVisibility(View.GONE);
                    baseViewHolder.getView(R.id.llAppItem).setVisibility(View.GONE);
                    baseViewHolder.getView(R.id.llContactItem).setVisibility(View.VISIBLE);
                    if (!TextUtils.isEmpty(bean.getName())) {
                        baseViewHolder.setText(R.id.tvContactsItemFirst, bean.getName().substring(0, 1));
                        baseViewHolder.setText(R.id.tvContactsItemName, bean.getName());
                    } else {
                        baseViewHolder.setText(R.id.tvContactsItemFirst, R.string.not_text);
                        baseViewHolder.setText(R.id.tvContactsItemName, R.string.not_have_name_text);
                    }
                    baseViewHolder.setText(R.id.tvContactsItemPhone, bean.getPath());
                    if (bean.isSelected()) {
                        baseViewHolder.setImageResource(R.id.ivContactsItemSel, R.drawable.axuanz);
                    } else {
                        baseViewHolder.setImageResource(R.id.ivContactsItemSel, R.drawable.aweixuanzhong);
                    }
                    if (isEdit) {
                        baseViewHolder.getView(R.id.ivContactsItemSel).setVisibility(View.VISIBLE);
                    } else {
                        baseViewHolder.getView(R.id.ivContactsItemSel).setVisibility(View.GONE);
                    }
                    break;

            }
        }
    }

    public static Drawable getApkIcon(Context context, String apkPath) {
        PackageManager pm = context.getPackageManager();
        PackageInfo info = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
        if (info != null) {
            ApplicationInfo appInfo = info.applicationInfo;
            appInfo.sourceDir = apkPath;
            appInfo.publicSourceDir = apkPath;
            try {
                return appInfo.loadIcon(pm);
            } catch (OutOfMemoryError e) {
                Log.e("ApkIconLoader", e.toString());
            }
        }
        return null;
    }

}
